package gxt.visual.presenter;

import gxt.visual.ui.client.interfaces.IButton;
import gxt.visual.util.Assert;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.data.BeanModel;

/**
 * @author eugenp
 */
public class ButtonsPresenter{
	private final List< IButton > buttonsManagedBySelection;
	private final List< IButton > buttonsManagedByServerCommunication;
	
	public ButtonsPresenter(){
		super();
		
		this.buttonsManagedBySelection = new ArrayList< IButton >();
		this.buttonsManagedByServerCommunication = new ArrayList< IButton >();
	}
	
	// API - public
	public void registerButtonManagedBySelection( final IButton button ){
		Assert.notNull( button );
		
		this.buttonsManagedBySelection.add( button );
	}
	public void registerButtonManagedByServerCommunication( final IButton button ){
		Assert.notNull( button );
		
		this.buttonsManagedByServerCommunication.add( button );
	}
	
	public void onServerCommunicationStarted(){
		for( final IButton buttonManagedByServerCommunication : this.buttonsManagedByServerCommunication ){
			buttonManagedByServerCommunication.disable();
		}
	}
	public void onServerCommunicationEnded(){
		for( final IButton buttonManagedByServerCommunication : this.buttonsManagedByServerCommunication ){
			buttonManagedByServerCommunication.enable();
		}
	}
	public void onSelectionActivated(){
		for( final IButton buttonManagedBySelection : this.buttonsManagedBySelection ){
			buttonManagedBySelection.enable();
		}
	}
	public void onSelectionDeactivated(){
		for( final IButton buttonManagedBySelection : this.buttonsManagedBySelection ){
			buttonManagedBySelection.disable();
		}
	}
	
	public void onMainSelectionChanged( final BeanModel beanModel ){
		if( beanModel == null ){
			this.onSelectionDeactivated();
		}
		else{
			this.onSelectionActivated();
		}
	}
	
}
